<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>易买网 - 管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Arial', sans-serif;
            background: #f5f5f5;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 1rem 2rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        .logo {
            font-size: 1.5rem;
            font-weight: bold;
        }
        
        .user-info {
            display: flex;
            align-items: center;
            gap: 1rem;
        }
        
        .user-info span {
            font-size: 0.9rem;
        }
        
        .logout-btn {
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: 1px solid rgba(255, 255, 255, 0.3);
            padding: 0.5rem 1rem;
            border-radius: 5px;
            cursor: pointer;
            transition: background 0.3s;
        }
        
        .logout-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }
        
        .container {
            max-width: 1200px;
            margin: 2rem auto;
            padding: 0 1rem;
        }
        
        .dashboard {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 2rem;
            margin-bottom: 2rem;
        }
        
        .card {
            background: white;
            padding: 2rem;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            text-align: center;
        }
        
        .card h2 {
            color: #333;
            margin-bottom: 1rem;
            font-size: 1.2rem;
        }
        
        .card p {
            color: #666;
            font-size: 2rem;
            font-weight: bold;
            margin-bottom: 1rem;
        }
        
        .nav {
            display: flex;
            gap: 1rem;
            margin-bottom: 2rem;
            flex-wrap: wrap;
        }
        
        .nav-btn {
            background: white;
            color: #333;
            border: 2px solid #667eea;
            padding: 1rem 2rem;
            border-radius: 8px;
            text-decoration: none;
            font-weight: bold;
            transition: all 0.3s;
            display: inline-block;
        }
        
        .nav-btn:hover {
            background: #667eea;
            color: white;
            transform: translateY(-2px);
        }
        
        .message {
            padding: 1rem;
            margin: 1rem 0;
            border-radius: 5px;
            text-align: center;
        }
        
        .error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="logo">易买网管理系统</div>
        <div class="user-info">
            <span id="userName"></span>
            <span id="userType"></span>
            <button class="logout-btn" onclick="logout()">退出登录</button>
        </div>
    </div>
    
    <div class="container">
        <div id="message"></div>
        
        <div class="dashboard">
            <div class="card">
                <h2>用户总数</h2>
                <p id="userCount">0</p>
                <small>注册用户数量</small>
            </div>
            
            <div class="card">
                <h2>商品总数</h2>
                <p id="productCount">0</p>
                <small>上架商品数量</small>
            </div>
            
            <div class="card">
                <h2>订单总数</h2>
                <p id="orderCount">0</p>
                <small>已完成订单</small>
            </div>
            
            <div class="card">
                <h2>新闻总数</h2>
                <p id="newsCount">0</p>
                <small>发布的新闻</small>
            </div>
        </div>
        
        <div class="nav">
            <a href="/users.html" class="nav-btn">用户管理</a>
            <a href="/products.html" class="nav-btn">商品管理</a>
            <a href="/orders.html" class="nav-btn">订单管理</a>
            <a href="/news.html" class="nav-btn">新闻管理</a>
        </div>
    </div>

    <script>
        let token = localStorage.getItem('token');
        let userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}');
        
        // 检查登录状态
        if (!token) {
            window.location.href = '/login.html';
        }
        
        // 显示用户信息
        document.getElementById('userName').textContent = userInfo.userName || '未知用户';
        document.getElementById('userType').textContent = userInfo.type === 1 ? '(管理员)' : '(普通用户)';
        
        // 加载统计数据
        async function loadStatistics() {
            try {
                const headers = {
                    'Authorization': 'Bearer ' + token
                };
                
                // 加载用户数量
                const usersResponse = await fetch('/api/users', { headers });
                if (usersResponse.ok) {
                    const usersResult = await usersResponse.json();
                    document.getElementById('userCount').textContent = usersResult.data.length;
                }
                
                // 加载商品数量
                const productsResponse = await fetch('/api/products', { headers });
                if (productsResponse.ok) {
                    const productsResult = await productsResponse.json();
                    document.getElementById('productCount').textContent = productsResult.data.length;
                }
                
                // 加载订单数量
                const ordersResponse = await fetch('/api/orders', { headers });
                if (ordersResponse.ok) {
                    const ordersResult = await ordersResponse.json();
                    document.getElementById('orderCount').textContent = ordersResult.data.length;
                }
                
                // 加载新闻数量
                const newsResponse = await fetch('/api/news', { headers });
                if (newsResponse.ok) {
                    const newsResult = await newsResponse.json();
                    document.getElementById('newsCount').textContent = newsResult.data.length;
                }
                
            } catch (error) {
                console.error('加载统计数据失败:', error);
                showMessage('加载数据失败，请检查网络连接', 'error');
            }
        }
        
        function showMessage(message, type) {
            const messageDiv = document.getElementById('message');
            messageDiv.innerHTML = `<div class="message ${type}">${message}</div>`;
            setTimeout(() => {
                messageDiv.innerHTML = '';
            }, 3000);
        }
        
        function logout() {
            localStorage.removeItem('token');
            localStorage.removeItem('userInfo');
            window.location.href = '/login.html';
        }
        
        // 验证token有效性
        async function validateToken() {
            try {
                const response = await fetch('/api/auth/validate', {
                    headers: {
                        'Authorization': 'Bearer ' + token
                    }
                });
                
                const result = await response.json();
                
                if (!result.data) {
                    // Token无效，跳转到登录页
                    localStorage.removeItem('token');
                    localStorage.removeItem('userInfo');
                    window.location.href = '/login.html';
                }
                
            } catch (error) {
                console.error('Token验证失败:', error);
                localStorage.removeItem('token');
                localStorage.removeItem('userInfo');
                window.location.href = '/login.html';
            }
        }
        
        // 页面加载时验证token并加载数据
        validateToken().then(loadStatistics);
    </script>
</body>
</html>